package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.ThumbnailGrid;
	import com.ettractions.kiosk.events.BrochureSelectEvent;
	import com.ettractions.kiosk.events.ViewEvent;
	import com.ettractions.kiosk.models.KioskModel;
	import com.ettractions.kiosk.vo.BrochureVO;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayList;
	import mx.core.IVisualElement;
	import mx.events.FlexEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.core.SpriteVisualElement;
	import spark.events.IndexChangeEvent;
	
	public class ThumbnailGridMediator extends Mediator
	{
		public static const WITHOUT_AD_LIST_ITEM_COUNT:uint = 40;
		
		public static const WITH_AD_LIST_ITEM_COUNT:uint = 35;
		
		[Inject]
		public var model:KioskModel;
		
		[Inject]
		public var view:ThumbnailGrid;
		
		private var _firstListItemIndex:int = 0;
		
		private var _adDisplayed:Boolean = false;
		
		private var _itemCount:uint = WITHOUT_AD_LIST_ITEM_COUNT;
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(ViewEvent.VIEW_INITIALIZED, handleViewInitialized);
			eventDispatcher.addEventListener(BrochureSelectEvent.BROCHURE_SELECTED, handleBrochureSelected);
			
			view.list.addEventListener(IndexChangeEvent.CHANGE, handleListIndexChange);
			view.list.addEventListener(FlexEvent.UPDATE_COMPLETE, handleListUpdateComplete);			
			
			view.previousButton.addEventListener(MouseEvent.CLICK, handleButtonClick);
			
			view.nextButton.addEventListener(MouseEvent.CLICK, handleButtonClick);
		}
		
		private function populateList():void
		{
			var listDataProvider:ArrayList = new ArrayList();
			
			_itemCount = _adDisplayed
			           ? WITH_AD_LIST_ITEM_COUNT
			           : WITHOUT_AD_LIST_ITEM_COUNT;
			
			for (var i:int=_firstListItemIndex; i<model.data.brochures.length; i++)
			{
				listDataProvider.addItem(model.data.brochures[i]);
				
				if (listDataProvider.length == _itemCount)
					break;
			}
			
			if (listDataProvider.length < _itemCount)
			{
				var addItemCount:uint = _itemCount - listDataProvider.length;
				
				for (i=0; i<addItemCount; i++)
					listDataProvider.addItem(model.data.brochures[i]);
			}
			
			view.list.dataProvider = listDataProvider;
		}
		
		private function handleViewInitialized(event:ViewEvent):void
		{
			eventDispatcher.removeEventListener(ViewEvent.VIEW_INITIALIZED, handleViewInitialized);
			
			populateList();
		}		
		
		private function handleBrochureSelected(event:BrochureSelectEvent):void
		{
			if (event.index > -1)
			{
				if (view.visible)
				{
					view.list.dataProvider = new ArrayList();

					view.visible = false;
				}
			}
			
			else
			{
				if (!view.visible)
				{
					_firstListItemIndex = 0;
					
					populateList();
					
					view.visible = true;
				}
			}
		}
		
		private function handleListIndexChange(event:IndexChangeEvent):void
		{
			model.selectBrochure(BrochureVO(view.list.dataProvider.getItemAt(event.newIndex)));
		}
		
		private function handleListUpdateComplete(event:FlexEvent):void
		{
			if (view.list.dataProvider.length > 0)
				eventDispatcher.dispatchEvent(new ViewEvent(ViewEvent.GRID_PAGE_CHANGE));
		}
		
		private function handleButtonClick(event:MouseEvent):void
		{
			switch (event.target)
			{
				case view.previousButton:
					_firstListItemIndex -= _itemCount;
					break;
				
				case view.nextButton:
					_firstListItemIndex += _itemCount;					
					break;				
			}
			
			if (_firstListItemIndex < 0)
				_firstListItemIndex = model.data.brochures.length + _firstListItemIndex;
				
			else if (_firstListItemIndex >= model.data.brochures.length)
				_firstListItemIndex = _firstListItemIndex - model.data.brochures.length;
			
			populateList();
		}
	}
}